package model;

import java.util.HashMap;

import dao.DaoException;
import dao.DaoFactory;
import dao.TokenDao;

/**
 * This class represents a token object
 * 
 * Tokens are key value combinations and each token belongs a particular
 * user who created it. System level tokens have id and user id as 0.
 * A user can not create a token that is one of the system tokens.
 * 
 * @author Musab M. Mirza, Sukhjinder K. Narula
 * @since May 2013
 *
 * @version $Revision: 1.0 $
 */
public class Token {
	
	/**
	 * Token dao
	 */
	private TokenDao dao;
		
	/**
	 * Token ID in the db
	 */
	private int id;
	
	/**
	 * User ID
	 */
	private int userId;
	
	/**
	 * Token key
	 */
	private String key;
	
	/**
	 * Token value
	 */
	private String value;
		
	/**
	 * Constructor
	 * 
	
	
	
	 * @param userId int
	 * @param key String
	 * @param value String
	 */
	public Token(int userId, String key, String value) {
		this.id = -1;
		this.userId = userId;
		this.key = key;
		this.value = value;
		this.dao = DaoFactory.getTokenDao();
	}

	/**
	 * Gets a token id
	
	 * @return id: numeric unique value of the token */
	public int getId() {
		return id;
	}

	/**
	 * Sets a token id
	
	 * @param id int
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Gets a user id
	
	 * @return userId: numeric unique value of the user id */
	public int getUserId() {
		return userId;
	}

	/**
	 * Sets the user id.
	
	 * @param userId int
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
	
	/**
	 * Gets a token key
	
	 * @return key: string value of the key of the token */
	public String getKey() {
		return key;
	}

	/**
	 * Sets a token key
	
	 * @param key String
	 */
	public void setkey(String key) {
		this.key = key;
	}

	/**
	 * Gets a token value
	
	 * @return value: string value of the value of the token */
	public String getValue() {
		return value;
	}

	/**
	 * Sets a token value
	
	 * @param value String
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * Returns the hash map representation of properties
	
	 * @return HashMap<String,String>
	 */
	public HashMap<String, String> getTransferObject(){
		
		HashMap<String, String> toReturn = new HashMap<String, String>();
		toReturn.put("Id", String.valueOf(id));
		toReturn.put("UserId", String.valueOf(userId));
		toReturn.put("Key", key);
		toReturn.put("Value", value);
		return toReturn;		
	}
	
	/**
	 * Saves (or creates new) token into the database
	
	 * @throws ModelException */
	public void save() throws ModelException{
		
		if (id == -1){
			try {
				id = dao.create(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		} else {
			try {
				dao.update(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		}
	}
	
	/**
	 * Deletes the current token from the database
	
	 * @throws ModelException  */
	public void delete() throws ModelException {
		try {
			dao.delete(id);
		} catch (DaoException e) {
			throw new ModelException(e.getMessage());
		}
	}

}
